package com.deruan.hotcore.resource.gui.tree;

import java.util.Iterator;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

import org.columba.contact.search.ContactSearchResult;
import org.columba.core.search.api.ISearchResult;
import org.columba.mail.search.MailSearchResult;

import com.deruan.hotcore.base.common.bean.ResourceBean;
import com.deruan.hotcore.resource.search.ResourceSearchResult;

public class ResultTreeModel extends DefaultTreeModel
{
	
	private ResourceTypeNode myResource = null;
	
	private ResourceTypeNode myCollect = null;
	
	public ResultTreeModel(List<ISearchResult> result, String providerName)
	{
		super(new ResultRootNode());
		if(providerName.equals("MailSearchProvider"))
		{
			createMailSearchTree(result);
		}
		else if(providerName.equals("ContactSearchProvider"))
		{
			createContactSearchTree(result);
		}
		else if(providerName.equals("ResourceSearchProvider"))
		{
			createResourceSearchTree(result);
		}
	}
	
	public ResultTreeModel()
	{
		super(new ResultRootNode());
	}
	
	public ResourceTypeNode getMyResource()
	{
		return myResource;
	}
	
	public void addMyResourceNode()
	{
		 myResource = new MyResourceType();
		((DefaultMutableTreeNode)(getRoot())).add(myResource);
	}
	
	public ResourceTypeNode getMyCollect()
	{
		return myCollect;
	}
	
	public void addMyCollectNode()
	{
		myCollect = new MyCollectedResource();
		((DefaultMutableTreeNode)(getRoot())).add(myCollect);
	}
	
	public void createResourceSearchTree(List<ISearchResult> result)
	{
		if(result!=null)
		{
			Iterator<ISearchResult> iterator = result.iterator();
			while(iterator.hasNext())
			{
				ResourceSearchResult searchResult = (ResourceSearchResult)iterator.next();
				ContactTreeNode contactNode = new ContactTreeNode(
						searchResult.getContactString(), searchResult.getResultNum(), true);
				((ResultTreeNode)getRoot()).add(contactNode);
				Iterator<ResourceBean> resoureIterator = searchResult.getResourceList().iterator();
				while (resoureIterator.hasNext()) {
					ResourceBean element = (ResourceBean) resoureIterator.next();
					contactNode.add(new ResourceTreeNode(element));
				}
			}
		}
	}
	
	public void createContactSearchTree(List<ISearchResult> result)
	{
		if(result!=null)
		{
			Iterator<ISearchResult> iterator = result.iterator();
			while(iterator.hasNext())
			{
				ContactSearchResult searchResult = (ContactSearchResult)iterator.next();
				ContactTreeNode contactNode = new ContactTreeNode(
						searchResult.getModel().getPreferredEmail(), null, true);
				((ResultTreeNode)getRoot()).add(contactNode);
			}
		}
	}

	public void createMailSearchTree(List<ISearchResult> result)
	{
		if(result!=null)
		{
			Iterator<ISearchResult> iterator = result.iterator();
			while(iterator.hasNext())
			{
				MailSearchResult searchResult = (MailSearchResult)iterator.next();
                MessageSearchNode node = new MessageSearchNode();
				node.setStatusIcon(searchResult.getStatusIcon());
				node.setTitle(searchResult.getTitle());
				node.setContactNameString(searchResult.getTitle());
				node.setFrom(searchResult.getFrom().getShortAddress());
				node.setDateString(searchResult.getStringDate());
				node.setFolder(searchResult.getFolder());
				node.setMessageIdObject(searchResult.getMessageId());
				((ResultTreeNode)getRoot()).add(node);
			}
		}
	}
	
	public void addTreeNode(TreeNode parentNode, TreeNode node)
	{
		((DefaultMutableTreeNode)parentNode).add((DefaultMutableTreeNode)node);
		 insertNodeInto((MutableTreeNode)node, (MutableTreeNode)parentNode, parentNode.getIndex(node));
	}
}
